#include "sio.h"


#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>


#define SIO_LIBNAME "SIO"


static int Lsio_close(lua_State *L) { return 0; }
static int Lsio_flush(lua_State *L) { return 0; }
static int Lsio_lines(lua_State *L) { return 0; }
static int Lsio_read(lua_State *L) { return 0; }
static int Lsio_write(lua_State *L) { return 0; }
static int Lsio___gc(lua_State *L) { return 0; }
static int Lsio___tostring(lua_State *L) { return 0; }

static int Lclose(lua_State *L) { return 0; }
static int Lflush(lua_State *L) { return 0; }
static int Linput(lua_State *L) { return 0; }
static int Linputfile(lua_State *L) { return 0; }
static int Llines(lua_State *L) { return 0; }
static int Loutput(lua_State *L) { return 0; }
static int Loutputfile(lua_State *L) { return 0; }
static int Lread(lua_State *L) { return 0; }
static int Lwrite(lua_State *L) { return 0; }


static void open_mt(lua_State *L) {
    luaL_Reg libs[] = {
#define ENTRY(name) { #name, Lsio_##name }
        ENTRY(close),
        ENTRY(flush),
        ENTRY(lines),
        ENTRY(read),
        ENTRY(write),
        ENTRY(__gc),
        ENTRY(__tostring),
#undef  ENTRY
        { NULL, NULL }
    };
    if (luaL_newmetatable(L, SIO_LIBNAME)) {
#if LUA_VERSION_NUM >= 502
        luaL_setfuncs(L, libs, 0);
#else
        luaL_register(L, NULL, libs);
#endif
    }
}

LUALIB_API int luaopen_sio(lua_State *L) {
    luaL_Reg libs[] = {
#define ENTRY(name) { #name, L##name }
        ENTRY(close),
        ENTRY(flush),
        ENTRY(input),
        ENTRY(inputfile),
        ENTRY(lines),
        ENTRY(output),
        ENTRY(outputfile),
        ENTRY(read),
        ENTRY(write),
#undef  ENTRY
        { NULL, NULL }
    };
    open_mt(L);
#if LUA_VERSION_NUM >= 502
    luaL_newlib(L, libs);
#else
    lua_createtable(L, 0, sizeof(libs)/sizeof(libs[0]));
    luaL_register(L, NULL, libs);
#endif
    return 1;
}
/* cc: flags+='-s -O3 -mdll -DLUA_BUILD_AS_DLL'
 * cc: libs+='-llua52.dll' input+='sio.c' output='sio.dll' */
